<?php
/*
 * Created on Aug 15, 2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class CodeLookupUtil {

	function getCodes($tableName)
	{
		$codeLookupDao = new CodeLookupDAO();
		return $codeLookupDao->findWhere("name = '$tableName'");
	}

	function getValue($tableName, $key)
	{
		$codeLookupDao = new CodeLookupDAO();
		$results =  $codeLookupDao->findWhere("name = '$tableName' and code_key = $key");
		if (count($results) == 0) return null;
		return $results[0]->value;
	}

	function getCode($tableName, $value)
	{
		$codeLookupDao = new CodeLookupDAO();
		$results = $codeLookupDao->findWhere("name = '$tableName' and value = '$value'");
		if (count($results) == 0) return null;
		return $results[0]->code_key;
	}

	function addCode($tableName, $value)
	{
		$codeLookupDao = new CodeLookupDAO();

		$maxCodeForTable = $codeLookupDao->findBySQL("select max(code_key) as code_key from code_lookup where name='$tableName'");
		$key = $maxCodeForTable[0]->code_key + 1;

		$newCode = new CodeLookup(0, $tableName, $key, $value);
		$codeLookupDao->save($newCode);

		return $key;
	}

}
?>
